import React, { useState } from 'react';
import { Button, message, Modal, Spin } from 'antd';
import axios from '@/components/service/request.js';
import { log } from '@craco/craco/lib/logger';

const ParseFile = ({ filePath }) => {
  const [loading, setLoading] = useState(false);
  const [loadingText, setLoadingText] = useState('');

  const handleParse = async () => {
    if (!filePath) {
      return message.error('请先上传文件');
    }

    setLoading(true);
    setLoadingText('正在解析文件，请稍候...');

    try {
      console.log('开始解析文件');
      const response = await axios.get('/model/run', { params: { filePath } });
      console.log(response);
      
      if (response.code === 200) {
        message.success('文件解析成功');
      } else if (response.code === 400) {
        message.error('文件格式错误，请检查文件');
      } else if (response.code === 404) {
        message.error('文件未找到，请上传文件后重试');
      } else {
        message.error(`解析失败，状态码: ${response.code}`);
      }
    } catch (error) {
      console.error('操作失败:', error);
      if (error.response) {
        message.error(`请求失败，状态码: ${error.response.status}`);
      } else {
        message.error('操作失败，请检查网络连接');
      }
    } finally {
      setLoading(false);
      setLoadingText('');
    }
  };

  return (
    <>
      <Button type="primary" onClick={handleParse} style={{ marginTop: 16 }} disabled={loading}>
        解析文件
      </Button>
      <Modal open={loading} footer={null} closable={false} maskClosable={false} centered>
        <Spin tip={loadingText} size="large" />
        <p>正在解析文件，请稍候···</p>
      </Modal>
    </>
  );
};

export default ParseFile;
